import 'package:easy_refresh/easy_refresh.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:largefriends/pages/lf_base_controller.dart';
import 'package:largefriends/pages/lf_profile/lf_profile_logic.dart';
import 'package:largefriends/res/lf_double_ext.dart';
import 'package:largefriends/res/strings/lf_strings.dart';
import 'package:largefriends/widgets/common_failed_page.dart';

import '../../../net/lf_api_util.dart';
import '../../../utils/lf_scroll_behavior.dart';
import '../../../widgets/user_card.dart';
import '../../lf_home/lf_home_logic.dart';
import '../lf_contact_empty.dart';
import '../lf_contact_list_loading.dart';
import '../lf_contact_sub_logic.dart';
import 'lf_faves_logic.dart';

class LfFavesPage extends StatefulWidget {
  const LfFavesPage({super.key});

  @override
  State<LfFavesPage> createState() => _LfFavesPageState();
}

class _LfFavesPageState extends State<LfFavesPage> {
  final logic = Get.put(LfFavesLogic());

  @override
  Widget build(BuildContext context) {
    return GetBuilder<LfFavesLogic>(
      builder: (logic) {
        if (logic.dataList.isEmpty) {
          if (logic.state == LoadingState.loading ||
              logic.state == LoadingState.init) {
            return const LfContactListLoading();
          } else if (logic.state == LoadingState.failed) {
            return CommonFailedPage(
              retry: () {
                logic.getCustomParamHttpRequest(isRefresh: true);
              },
            );
          }
          return LfContactEmptyWidget(
            title: strNoAnyFavoritesTip.tr,
            actionTitle: strSearchMatches.tr,
            action: () {
              LfHomeLogic homeLogic = Get.find();
              homeLogic.pageController.jumpTo(0);
              homeLogic.selectedIndex.value = 0;
            },
          );
        }
        return Padding(
          padding: EdgeInsets.only(top: 10.h),
          child: ScrollConfiguration(
              behavior: LfScrollBehavior(),
              child: EasyRefresh(
                  controller: logic.refreshController,
                  onLoad: () async {
                    var result =
                        await logic.getCustomParamHttpRequest(isRefresh: false);
                    return result;
                  },
                  onRefresh: () async {
                    var result =
                        await logic.getCustomParamHttpRequest(isRefresh: true);
                    return result;
                  },
                  child: CustomScrollView(
                    slivers: [
                      SliverPadding(
                        padding: EdgeInsets.symmetric(horizontal: 10.w),
                        sliver: SliverList.separated(
                          itemBuilder: (context, index) {
                            var userEntity = logic.dataList[index];
                            return InkWell(
                              child: UserCardWidget(
                                userEntity: userEntity,
                                showNew: false,
                                needBlur: false,
                              ),
                              onTap: () {
                                toProfilePage(
                                    uid: userEntity.userId!,
                                    userEntity: userEntity,
                                    type: ContactType.myFave);
                              },
                              onLongPress: () {
                                unlikeUser(user: userEntity);
                              },
                            );
                          },
                          separatorBuilder: (context, index) {
                            return 15.h.sbh;
                          },
                          itemCount: logic.dataList.length,
                        ),
                      )
                    ],
                  ))),
        );
      },
    );
  }
}
